package cn.kz.hrm.web.controller;

import cn.kz.hrm.dto.CourseDto;
import cn.kz.hrm.service.ICourseService;
import cn.kz.hrm.domain.Course;
import cn.kz.hrm.query.CourseQuery;
import cn.kz.hrm.util.AjaxResult;
import cn.kz.hrm.util.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/course")
public class CourseController {
    @Autowired
    public ICourseService courseService;


    @RequestMapping(value = "/getCourseById/{id}",method = RequestMethod.GET)
   public AjaxResult getCourseById(@PathVariable("id") Long id){
       CourseDto courseDto = courseService.getCourseById(id);
        return AjaxResult.me().setResultObj(courseDto);
    }


    /**
     * 根据查询对象对课程的进行类表的显示
     * @return
     */
    @RequestMapping(value="/queryCourses",method= RequestMethod.POST)
    public AjaxResult getQueryCourses(@RequestBody CourseQuery courseQuery ){
        try {
            return courseService.getQueryCourses(courseQuery);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("课程分类查询失败！"+e.getMessage());
        }
    }


    /**
     * 对课程进行下线，同时要从ElasticSearch中进行删除
     * @param id
     * @return
     */
    @RequestMapping(value="/offLineCourse/{id}",method= RequestMethod.POST)
    public AjaxResult offLineCourse(@PathVariable("id") Long id){
        try {
            courseService.offLineCourse(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("课程上线异常，未知原因！！"+e.getMessage());
        }
    }


    /**
     * 对课程上线，先修改数据库中的状态，然后存入到ElasticSearch
     * @param id
     * @return
     */
    @RequestMapping(value="/onLineCourse/{id}",method= RequestMethod.POST)
    public AjaxResult onLineCourse(@PathVariable("id") Long id){
        try {
            courseService.onLineCourse(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("课程上线异常，未知原因！！"+e.getMessage());
        }
    }

    /**
    * 保存和修改公用的
    * @param courseDto  传递的实体
    * @return Ajaxresult转换结果
    */
    @RequestMapping(value="/saveOrUpdate",method= RequestMethod.POST)
    public AjaxResult save(@RequestBody CourseDto courseDto){
        try {
            courseService.saveOrUpdate(courseDto);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存课程及课程营销以及课程类型失败！！"+e.getMessage());
        }
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            courseService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }

    //获取用户
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public Course get(@PathVariable("id")Long id)
    {
        return courseService.selectById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public List<Course> list(){

        return courseService.selectList(null);
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public PageList<Course> json(@RequestBody CourseQuery query)
    {
        Page<Course> page = new Page<Course>(query.getPage(),query.getRows());
        page = courseService.selectPage(page);
        return new PageList<Course>(page.getTotal(),page.getRecords());
    }
}
